using Abp.Domain.Services;
using SOEI.Solution.Authorization.Users;
using SOEI.Solution.Customized;
using SOEI.Solution.DomainEntities.Organization;
using SOEI.Solution.Helper;
using SOEI.Solution.Integratedes.Organization;
using System;
using System.Threading.Tasks;

namespace SOEI.Solution
{
    public abstract class DomainServiceBase : DomainService
    {
        /*在领域服务中添加你的自定义公共方法*/
        protected DomainServiceBase()
        {
            LocalizationSourceName = SolutionConsts.LocalizationSourceName;
        }

        public virtual User CurrentUser
        {
            get
            {
                var vUser = HttpContext.Current.Items[KeyNames.CurrentUser];

                if (vUser == null)
                {
                    vUser = (HttpContext.Current.Items[KeyNames.CurrentUserFunc] as Func<User>)();

                    HttpContext.Current.Items[KeyNames.CurrentUser] = vUser;
                }

                return vUser as User;
            }
        }

        #region -------------------------------- 实体转换 -------------------------------
        /// <summary>
        /// 本地经济机构实体 转换为综合系统经济实体
        /// </summary>
        /// <param name="entity">本地系统经济机构</param>
        /// <returns>综合系统经济机构</returns>
        public OEconomyOrgani ConvertToOEconomyOrgani(EconomyOrgani entity)
        {
            var OEntity = ObjectMapper.Map<OEconomyOrgani>(entity);
            OEntity.Id = entity.JGBH; // 
            return OEntity;
        }

        /// <summary>
        /// 机构人员实体转为机构变更实体
        /// </summary>
        /// <param name="entity">本地系统经济机构</param>
        /// <returns>综合系统经济机构</returns>
        public OEconomyPersonChange ConvertToOEconomyPersonChange(OEconomyPerson entity)
        {
            var OEntity = ObjectMapper.Map<OEconomyPersonChange>(entity);
            OEntity.RYBH = entity.Id; // 设置人员编号
            return OEntity;
        }

        /// <summary>
        /// 返回提交给综合系统的申请状态枚举的字符串格式 
        /// </summary>
        /// <param name="ApprovalStatus"></param>
        /// <returns></returns>
        public string GetApprovalStatus(EApprovalStatus ApprovalStatus)
        {
            return ((int)ApprovalStatus).ToString();
        }

        #endregion


        #region -------------------------------------------------用户自定义------------------------------------------------

        /// <summary>
        /// 获取一个文件Guid 如果为空或者空的GUID则返回一个新的GUID
        /// </summary>
        /// <param name="attachId"></param>
        /// <returns></returns>
        public Guid GetAttachGuid(Guid attachId) 
        {
            if (attachId == Guid.Empty) {
                return Guid.NewGuid();
            }
            return attachId;
        }

        #endregion
    }
}
